package com.mazaiting.easy.base.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mazaiting.easy.R;
import com.mazaiting.easy.app.BaseApplication;
import com.mazaiting.easy.base.interfaces.IPermissionStrategy;
import com.mazaiting.easy.base.interfaces.IView;
import com.mazaiting.easy.base.mvp.IBaseView;
import com.mazaiting.easy.base.presenter.BasePresenter;
import com.mazaiting.log.L;
import com.mazaiting.permission.PermissionResult;
import com.mazaiting.permission.util.PermissionUtil;
import com.mazaiting.permission.util.State;
import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrInterface;
import com.r0adkll.slidr.model.SlidrPosition;
import com.trello.rxlifecycle3.LifecycleProvider;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import javax.inject.Inject;
import java.util.List;

/**
 * Activity基类
 *
 * @author mazaiting
 * @date 2018/2/5
 */
public abstract class BaseActivity<T extends BasePresenter>
        extends RxAppCompatActivity implements IBaseView, IView {
    /**
     * 权限请求码
     */
    public static final int REQUEST_PERMISSION_CODE = 0x100;
    /**
     * 根布局
     */
    protected View mRootView;
    /**
     * 主持人
     */
    @Nullable
    @Inject
    T mPresenter;
    /**
     * 权限策略
     */
    private IPermissionStrategy mPermissionStrategy = getPermissionStrategy();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 创建布局
        mRootView = getLayoutInflater().inflate(getContentLayout(), null);
        // 设置布局内容
        setContentView(mRootView);
        // 注入ApplicationComponent
        inject(BaseApplication.getInstance().getApplicationComponent());
        // 绑定布局--与Presenter
        attachView();
        // 绑定布局
        bindView(mRootView, savedInstanceState);
        // 初始化数据
        initData();
        // 初始化侧滑关闭
        initSlide();
        // 请求权限
        if (null != mPermissionStrategy) PermissionUtil.INSTANCE.requestPermission(this);
    }

    /**
     * 初始化侧滑
     */
    private void initSlide() {
        // 配置侧滑返回
        SlidrInterface slidrInterface = Slidr.attach(this, getSlidrConfig());
        // 是否禁用侧滑
        if (isSlideLock()) slidrInterface.lock();
    }

    /**
     * 获取侧滑配置
     *
     * @return 侧滑配置
     */
    protected SlidrConfig getSlidrConfig() {
        // 侧滑配置
        SlidrConfig.Builder builder = new SlidrConfig.Builder()
                .edge(false) // true 代表边界   false全屏触摸
                .edgeSize(1.0f)
                .scrimColor(Color.TRANSPARENT) // 侧滑颜色
                .position(SlidrPosition.LEFT) // 位置
                .scrimStartAlpha(0.8f) // 开始透明度
                .scrimEndAlpha(0f)  // 结束透明度
                .velocityThreshold(10f) // 超过这个滑动速度，忽略位移限定值就切换Activity
                .distanceThreshold(.35f); // 滑动位移占屏幕的百分比，超过这个间距就切换Activity
        return builder.build();
    }

    /**
     * 是否允许侧滑
     *
     * @return true: 允许; false: 禁止
     */
    protected boolean isSlideLock() {
        return false;
    }

    /**
     * 获取权限策略
     *
     * @return 权限策略
     */
    protected IPermissionStrategy getPermissionStrategy() {
        return null;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (null != mPermissionStrategy) {
            // 权限处理
            PermissionUtil.INSTANCE.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        }
    }

    /**
     * 权限处理
     *
     * @param state       权限状态
     * @param permissions 权限列表: 注意可为空
     */
    @PermissionResult(REQUEST_PERMISSION_CODE)
    private void permissionResult(State state, List<String> permissions) {
        if (null != mPermissionStrategy) {
            switch (state) {
                case NOT_SHOW:
                    // 勾选不再显示
                    mPermissionStrategy.onNotShow(permissions);
                    break;
                case SUCCESS:
                    // 请求成功
                    mPermissionStrategy.onSuccess();
                    break;
                case DENIED:
                    // 拒绝
                    mPermissionStrategy.onDenied(permissions);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public View getView() {
        return mRootView;
    }

    /**
     * 获取主持人
     *
     * @return 主持人
     */
    @Nullable
    @Override
    public T getPresenter() {
        return mPresenter;
    }

    /**
     * 绑定布局
     */
    private void attachView() {
        if (null != mPresenter) {
            mPresenter.attachView(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 与Presenter解除绑定
        if (null != mPresenter) {
            mPresenter.detachView();
        }
    }

    @Override
    public LifecycleProvider getLifecycleProvider() {
        return this;
    }


    /**
     * 退出应用, 点击返回键不退出应用
     */
    protected void quitApplication() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

}
